package com.erp.erp_project.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.erp.erp_project.mapper.ProductMapper;
import com.erp.erp_project.model.Order;
import com.erp.erp_project.model.Product;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class ProductService implements IProductService{
    
    private ProductMapper productMapper;

    @Override
    public List<Product> getAll() {
        QueryWrapper<Product> wrapper=new QueryWrapper<>();
        wrapper.isEmptyOfWhere();
        return productMapper.selectList(wrapper);
    }

    @Override
    public Product getProductById(Integer id) {
        return productMapper.selectById(id);
    }

    @Override
    public void updatePic(Integer id, String data) {
        productMapper.updateImage(id,data);
    }

    @Override
    public void addProduct(Product product) {
        productMapper.insert(product);
    }

    @Override
    public void editProduct(Product product) {
        productMapper.updateById(product);
    }

    @Override
    public void deleteProduct(List<Integer> list) {
        QueryWrapper<Product> wrapper=new QueryWrapper<>();
        wrapper.in("product_id",list);
        productMapper.delete(wrapper);
    }

    @Override
    public List<Product> searchProduct(Integer type, String condition) {
        condition="%"+condition+"%";
        return productMapper.search(condition);
    }

    @Override
    public void setNote(String note, Integer id) {
        productMapper.updateNote(note,id);
    }

    @Override
    public List<String> uploadPic(MultipartFile[] files, Integer id) {
        StringBuilder builder=new StringBuilder();
        List<String> list=new ArrayList<>();
        for (MultipartFile file: files) {
            String oldName=file.getOriginalFilename();
            Long newName=System.currentTimeMillis();
            String folderPath="D:/Picture/pic/product/"+id+"/";
            String realPath=folderPath+newName+oldName.substring(oldName.lastIndexOf("."));
            File folder=new File(folderPath);
            if (!folder.exists()){
                folder.mkdirs();
            }
            File file1=new File(realPath);
            try {
                file.transferTo(file1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            String path="/img/product/"+id+"/"+newName+oldName.substring(oldName.lastIndexOf("."));
            builder.append(","+path);
            list.add(path);
        }
        if (id!=null){
            Product product=productMapper.selectById(id);
            String data="";
            if (product.getImage()!=null&&!product.getImage().equals("")){
                data=product.getImage()+builder;
            }else {
                data= builder.substring(1);
            }
            productMapper.updatePic(id,data);
        }
        return list;
    }
}
